function anglemap = GenerateAngleMap(th, varargin)
% Generate an direction or orientation angle map
% th: preferred angle obtained by vector averaging, normalized to 0-1 
%       (result of e.g. CalcMapParams).
%
% optional parameters
% 'ColorShift', value
% 'OutputPath', 'pathname' (in this case angle map will be saved)

colorShift = 60/360; % this gives blue as horizontal orientation, assuming that the hue_table starts with red by default
pathname = [];

% process optional parameters
if ~isempty(varargin)
    numIndex = find(cellfun('isclass', varargin(1:end-1), 'char'));
    for ind = 1:length(numIndex)
        switch lower(varargin{numIndex(ind)})
            case 'colorshift'
                colorShift = varargin{numIndex(ind) + 1};
            case 'outputpath'
                pathname = varargin{numIndex(ind) + 1};
        end
    end
end

dim = size(th);
anglemap = zeros(dim(1),dim(2),3);

th = mod(th + colorShift, 1 );
for x = 1:dim(1)
    for y = 1:dim(2)
        anglemap(x,y,:) = HsvToRgb(th(x,y),1,1)';  %ML: to get same color as in cell maps
%         anglemap(x,y,:) = hsv2rgb([th(x,y),1,1]);  % matlab in-built function
    end
end

if ~isempty(pathname)
    imwrite(anglemap, fullfile(pathname, 'angle.tif'));
end
